package cn.hxzy.service;

import cn.hxzy.dao.UserDao;
import cn.hxzy.entity.News;
import cn.hxzy.entity.User;
import cn.hxzy.entity.vo.PageObj;

import java.sql.SQLException;
import java.util.List;

public class UserService {
    UserDao userDao = new UserDao();

//    public List<User> findAll() throws Exception {
//        return userDao.findAll();
//    }
//    public PageObj<User> findAll(PageObj<User> pageObj) throws Exception {
//
//    }

    public User login(String loginName, String loginPassword) throws Exception {
        User user = userDao.findByLoginName(loginName);
        if (user != null && user.getPassword().equals(loginPassword)) {
            return user;
        }
        return null;
    }

    public User findById(int id) throws Exception {
        return userDao.findById(id);
    }

    public void add(User user) throws Exception {
        userDao.add(user);
    }

    /**
     * 查询id分页
     */
    public PageObj<User> findByIdWithUser(PageObj<User> page, int id) throws Exception {
        try {
            List<User> all = userDao.findByIdLike(page.getPage(), page.getSize(), id);
            page.setList(all);
            long count = userDao.countAllWithId(id);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
//        return userDao.findByIdWithUser(id);
    }

    public PageObj<User> findByNameWithUser(PageObj<User> page,String name) throws SQLException {
        try {
            List<User> all = userDao.findByNameLike(page.getPage(), page.getSize(), name);
            page.setList(all);
            long count = userDao.countAllWithName(name);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;

    }

    /**
     *删除用户
     */
    public void delete(int id) throws Exception {
        userDao.delete(id);
    }

    /**
     *修改用户
     */
    public void update(User user) throws Exception {
        userDao.update(user);
    }

    public User findByUserNumber(int id) throws Exception {
        List<User> users = userDao.findAll();
        for (User user : users) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }
/**
 *查询全部信息
 */
    public List<User> findAll() throws Exception {
        return userDao.findAll();
    }

    public PageObj<User> findAll(PageObj<User> pageObj) throws Exception {
        List<User> all = userDao.findAll(pageObj.getPage(), pageObj.getSize());
        pageObj.setList(all);
        long count = userDao.countAll();
        pageObj.setCount(count);
        Integer size = pageObj.getSize();
        long l = count % size != 0 ? (count / size) + 1 : (count / size);
        pageObj.setPageCount((int) l);
        return pageObj;
    }

}
